/*
 * Copyright 2011 Biz-e (info@biz-e.nl)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.bize.knexions.android.knexion.twitter;

import java.util.List;

import nl.bize.knexions.android.AndroidKnexionsApplication;
import nl.bize.knexions.android.knexion.AccountException;
import nl.bize.knexions.android.knexion.DataStorage;

/**
 * Static class to manage storage of twitter accounts
 * 
 * @author Joost A. Bloemsma
 */
public class TwitterAccountStore {
	private static DataStorage ds;
	
	static {
		ds = new DataStorage(AndroidKnexionsApplication.getAppContext());
	}
	
	/**
	 * Constructs a new TwitterAccountStore instance
	 * Declared private so it cannot be called
	 */
	private TwitterAccountStore() {}
	
	/**
	 * Fills the passed {@link TwitterAccount} instance with data belonging to the user.
	 * The username must be set already.
	 * 
	 * @param account
	 * @return true if the account exists and data is filled
	 */
	public static boolean fill(TwitterAccount account) {
		return ds.fillStorable(account);
	}
	
	/**
	 * Gets the {@link TwitterAccount} instance that has been made persistent with the 
	 * specified name. If no data can be found, an {@link AccountException} is thrown.
	 * @param name
	 * @return An instance of {@link TwitterAccount} filled with the info
	 * @throws AccountException
	 */
	public static TwitterAccount getAccount(String name) throws AccountException {
		TwitterAccount ta = new TwitterAccount();
		ta.setName(name);
		if (!ds.fillStorable(ta)) {
			throw new AccountException("No info found for account " + name);
		}
		return ta;
	}
	
	/**
	 * Makes the passed {@link TwitterAccount} persistent.
	 * @param ta The {@link TwitterAccount} to store
	 * @return true if the account exists
	 */
	public static boolean storeAccount(TwitterAccount ta) {
		return ds.storeObject(ta);
	}
	
	/**
	 * Returns a {@link List<String>} of all currently persisted Twitter Accounts
	 * @return A list of Twitter account names
	 */
	public static List<String> getNames() {
		return ds.getAllNames(TwitterAccount.CTX);
	}
}

